package codetop;

import java.util.HashSet;
import java.util.Set;

public class Code3 {
    public int lengthOfLongestSubstring(String s) {
        if (s.length()==0)return 0;
        Set<Character> set=new HashSet<>();
        int left=0;
        int right=0;
        int max=0;
        while (right<s.length()){
            char c=s.charAt(right);
            if (set.contains(c)){
                while (left<right){
                    char c_left=s.charAt(left);
                    if (c_left==c){
                        left++;
                        right++;
                        break;
                    }else {
                        set.remove(c_left);
                        left++;
                    }
                }
            }else {
                set.add(c);
                right++;
                max=Math.max(max,right-left);
            }
        }
        return max;
    }
}
